<template>
  <div>
    <van-picker
      title="修改性别"
      show-toolbar
      @cancel="$emit('close')"
      :columns="columns"
      :default-index="gender"
      @change="onChange"
      @confirm="updateGender"
    />
  </div>
</template>

<script>
import { updateProfileAPI } from '@/api'
export default {
  name: '',
  props: {
    gender: {
      type: Number,
      default: null
    }
  },
  data () {
    return {
      columns: ['男', '女'],
      genderIndex: null
    }
  },
  methods: {
    onChange (picker, value, index) {
      this.genderIndex = index
      console.log(this.genderIndex)
    },
    onConfirm () {
      this.updateGender()
    },
    async updateGender () {
      try {
        await updateProfileAPI({
          gender: this.genderIndex
        })
        this.$emit('update:gender', this.genderIndex)
        this.$emit('close')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>
</style>